#include "maajanHand.h"
#include <stddef>


// ----------- helper functions -----------



// ----------------------------------------


maajanHand::maajanHand(maajanTile newValue[]) {
	hand = new maajanTile[TILES_NUM_HAND + 1]; // +1 for new cards
	meldlist = new maajanTile[TILES_NUM_HAND + 1]; // +1 for all 13 cards
	
	for (int i = 0; i < TILES_NUM_HAND; i++) {
		hand[i] = newvalue[i];
	}
	
	meldList = NULL;
	quantityHand = TILES_NUM_HAND;
	quantityMeldrist = 0;
	
}

maajanHand::~maajanHand() {
	delete hand[];
	
}


// ----------------------------------------


void maajanHand::setHand(maajanTile newValue[]) {
	for (int i = 0; i < TILES_NUM_HAND; i++) {
		hand[i] = newValue[i];
	
	quantityHand = TILES_NUM_HAND;
	
}

int maajanHand::getCardIndex(int index) {
	return hand[index]->index;

}

int maajanHand::getQtyHand() {
	return quantityHand;
	
}

int maajanHand::getQtyMeldlist() {
	return quantityMeldlist;
	
}


// ----------------------------------------


void maajanHand::discardTile(const int &index) {
	hand[index] = NULL;
	shift(index);
	
	if (hand[quantityHand] == hand[quantityHand - 1] {
		hand[quantityHand = NULL;
	}
	
	return;
	
}

void insertTile(const maajanTile &newValue) {
	int index;
	for (int index = 0; index < quantityHand && newValue > hand[index]; index++) {}
	
	for (int i = quantityHand; i > index; i--) {
		hand[i] = hand[i-1];
	}
	
	hand[indexx] = newValue;
	
}

void removeTile(const int &index) {
	hand[index] = NULL;
	shift(index);
	
	return;
	
}


// ----------------------------------------


void maajanHand::sortList(const int &list) { // sort by total order
	// *** NOT IMPLEMENTED YET *** //
	
	/*
	int count;
	maajanTile tempHand[9];
	maajanTile &target;
	
	for (int i = 0; i < TILES_NUM_HAND) {
	
	target = &(*hand)[index];
	if (curr 
	count = i;
		while (count > 0 && target > curr
	
	// ...
	
	}
	
	*/
	
	return;
	
}

void maajanHand::insertTile (const maajanTile &newValue) {
	int temp = 0;
	
	while (temp < TILES_NUM_HAND && newValue > hand[index]) {}
	
	for (int i = TILES_NUM_HAND - 1; i > temp; i--) {
		hand[i] = hand[i - 1];
	
	}
	
	hand[temp] = &newValue;
	
	return;
}


// ----------------------------------------


void maajanHand::reset() {
	hand = NULL;
	meldlist = NULL;
	quantityHand = 0;
	quantityMeldlist = 0;

	return;
	
}


// ----------------------------------------


void shift(const int &index) {
	for (; index < TILES_NUM_HAND - 1; index++) {
		hand[index] = hand[index + 1];
		
	}
	
	return;
	
}

